package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import net.maku.events.dao.UserCommentDao;
import net.maku.events.entity.UserCommentEntity;
import net.maku.events.query.UserCommentQuery;
import net.maku.events.service.UserCommentService;
import net.maku.events.vo.UserCommentVO;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: qr
 * @Date: 2025/3/27 22:52
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
public class UserCommentServiceImpl extends BaseServiceImpl<UserCommentDao, UserCommentEntity> implements UserCommentService {


    //    @Override
//    public List<UserCommentVO> getList(UserCommentQuery query) {
//        IPage<UserCommentEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
//        List<UserCommentVO> result = BeanUtil.copyToList(page.getRecords(), UserCommentVO.class);
//        return new PageResult<>(result, page.getTotal());
//    }
    @Override
    public List<UserCommentVO> getList(UserCommentQuery query) {
        return BeanUtil.copyToList(this.list(getWrapper(query)), UserCommentVO.class);
    }

    private LambdaQueryWrapper<UserCommentEntity> getWrapper(UserCommentQuery query) {
        LambdaQueryWrapper<UserCommentEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserCommentEntity::getBusinessId, query.getBusinessId())
                .eq(UserCommentEntity::getBusinessType, query.getBusinessType()).orderByDesc(UserCommentEntity::getCreateTime);
        return wrapper;
    }


    @Override
    public void saveComment(Long businessId, Integer businessType, String commentContents) {
        UserCommentEntity entity = new UserCommentEntity();
        entity.setBusinessId(businessId);
        entity.setBusinessType(businessType);
        entity.setCommentContents(commentContents);
        entity.setUserId(SecurityUser.getUserId());
        this.save(entity);
    }
}
